/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO_BUS;

import java.util.Vector;
import org.w3c.dom.Document;

/**
 *
 * @author MinhNhat
 */
public class DeleteSentence extends MySentence {
    // DELETE FROM Persons
    // WHERE LastName='Tjessem' AND FirstName='Jakob'

    String Table;
    Vector Conditions;

    public DeleteSentence(String strDelete) {
        Table = "";
        Conditions = new Vector();
        strDelete = strDelete.trim();
        strDelete = strDelete.replaceAll("(\r\n|\n)", " ");

        if (!strDelete.isEmpty()) {
            strDelete = GetTable(strDelete);
        }
        if (!strDelete.isEmpty()) {
            Conditions = GetConditions(strDelete);
        }
    }

    public String GetTable(String strDelete) {
        strDelete = strDelete.trim();
        String s1 = strDelete.toLowerCase();
        String s2 = "from";
        int indexOfTableName = s1.indexOf(s2);


        if (indexOfTableName == -1) {
            return strDelete;
        }

        String s3 = "where";
        int indexOfEndTableName = s1.indexOf(s3);
        if (indexOfEndTableName == -1) {
            indexOfEndTableName = strDelete.length();

        }

        Table = strDelete.substring(indexOfTableName + s2.length(), indexOfEndTableName).trim();

        return strDelete.substring(indexOfEndTableName, strDelete.length());
    }

    public static boolean isDeleteSentence(String str) {
        String s1 = str.toLowerCase().trim();
        String s2 = "delete ";
        int indexOfString = s1.indexOf(s2);
        if (indexOfString == 0) {
            return true;
        }
        return false;
    }

    public boolean Excute(Document doc, Vector Tables) {
        try {
            if (Table == null) {
                return false;
            }
            for (int i = 0; i < Tables.size(); i++) {
                MyTable table = (MyTable) Tables.get(i);
                if (table.GetName().compareTo(Table) == 0) {
                    return table.DeleteData(Conditions);

                }
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return false;
        }
        return false;
    }

    public void Print() {

        for (int i = 0; i < Conditions.size(); i++) {
            MyExpression express = (MyExpression) Conditions.get(i);
            express.Print();
        }
    }
}
